<template>
  <Dialog v-model="dialogVisible" title="客户查重字段">
    <el-text type="warning"> 根据勾选的字段进行查重，例如客户名称+邮箱</el-text>
    <div v-for="column in columns" :key="column.type">
      <div class="font-bold mt-3">{{ column.typeName }}</div>
      <div style="display: flex; flex-wrap: wrap; justify-content: center">
        <div style="width: 200px" v-for="(columnField, index) in column.columnInfos" :key="index">
          <el-checkbox v-model="formData[columnField.name]" style="margin-right: 4px" />
          <span>{{ columnField.showName }}</span>
        </div>
      </div>
    </div>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="CustomerDuplicateCheckSetting" lang="ts">
const dialogVisible = ref(false)
import * as CustomerCheckApi from '@/api/customer/customerCheck'
import { SettingBusinessModuleEnum } from '@/utils/constants'
import * as BusinessFieldApi from '@/api/setting/businessField'
import { getKeyList } from '@pureadmin/utils'
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const formData = ref({})
const columns = ref([
  {
    type: 'customer',
    businessModule: SettingBusinessModuleEnum.CUSTOMER,
    tableName: 'customer_info',
    typeName: '客户',

    columnInfos: [
      {
        name: 'name',
        showName: '名称',
        value: false
      },
      {
        name: 'short_name',
        showName: '简称',
        value: false
      },
      {
        name: 'address',
        showName: '地址',
        value: false
      },
      {
        name: 'website',
        showName: '网址',
        value: false
      },
      {
        name: 'custom_field1',
        showName: '自定义字段1',
        value: false
      },
      {
        name: 'custom_field2',
        showName: '自定义字段2',
        value: false
      },
      {
        name: 'custom_field3',
        showName: '自定义字段3',
        value: false
      },
      {
        name: 'custom_field4',
        showName: '自定义字段4',
        value: false
      },
      {
        name: 'custom_field5',
        showName: '自定义字段5',
        value: false
      },
      {
        name: 'custom_field6',
        showName: '自定义字段6',
        value: false
      },
      {
        name: 'custom_field7',
        showName: '自定义字段7',
        value: false
      },
      {
        name: 'custom_field8',
        showName: '自定义字段8',
        value: false
      },
      {
        name: 'custom_field9',
        showName: '自定义字段9',
        value: false
      },
      {
        name: 'custom_field10',
        showName: '自定义字段10',
        value: false
      },
      {
        name: 'custom_field11',
        showName: '自定义字段11',
        value: false
      },
      {
        name: 'custom_field12',
        showName: '自定义字段12',
        value: false
      },
      {
        name: 'custom_field13',
        showName: '自定义字段13',
        value: false
      },
      {
        name: 'custom_field14',
        showName: '自定义字段14',
        value: false
      },
      {
        name: 'custom_field15',
        showName: '自定义字段15',
        value: false
      },
      {
        name: 'custom_field16',
        showName: '自定义字段16',
        value: false
      },
      {
        name: 'custom_field17',
        showName: '自定义字段17',
        value: false
      },
      {
        name: 'custom_field18',
        showName: '自定义字段18',
        value: false
      },
      {
        name: 'custom_field19',
        showName: '自定义字段19',
        value: false
      },
      {
        name: 'custom_field20',
        showName: '自定义字段20',
        value: false
      }
    ]
  },
  {
    type: 'customerContact',
    tableName: 'customer_contact',
    businessModule: SettingBusinessModuleEnum.CUSTOMER_CONTACT,
    typeName: '客户联系人',
    columnInfos: [
      {
        name: 'contact_name',
        showName: '姓名',
        value: false
      },
      {
        name: 'contact_nick_name',
        showName: '昵称',
        value: false
      },
      {
        name: 'contact_phone',
        showName: '电话',
        value: false
      },
      {
        name: 'contact_mail',
        showName: '邮箱',
        value: false
      },
      {
        name: 'contact_social_media',
        showName: '社交媒体',
        value: false
      },
      {
        name: 'contact_address',
        showName: '地址',
        value: false
      },
      {
        name: 'contact_custom_field1',
        showName: '自定义字段1',
        value: false
      },
      {
        name: 'contact_custom_field2',
        showName: '自定义字段2',
        value: false
      },
      {
        name: 'contact_custom_field3',
        showName: '自定义字段3',
        value: false
      },
      {
        name: 'contact_custom_field4',
        showName: '自定义字段4',
        value: false
      },

      {
        name: 'contact_custom_field5',
        showName: '自定义字段5',
        value: false
      },
      {
        name: 'contact_custom_field6',
        showName: '自定义字段6',
        value: false
      },
      {
        name: 'contact_custom_field7',
        showName: '自定义字段7',
        value: false
      },
      {
        name: 'contact_custom_field8',
        showName: '自定义字段8',
        value: false
      },
      {
        name: 'contact_custom_field9',
        showName: '自定义字段9',
        value: false
      },
      {
        name: 'contact_custom_field10',
        showName: '自定义字段10',
        value: false
      }
    ]
  }
])
const open = async () => {
  dialogVisible.value = true
  formData.value = {}
  const checkColumns = await CustomerCheckApi.settingQuery()
  const columnIds = getKeyList(checkColumns, 'columnName')
  columns.value.forEach(async (column) => {
    const moduleField = await BusinessFieldApi.getByModule(column.businessModule)
    const moduleFieldName = getKeyList(moduleField, 'fieldName')
    const moduleFieldShowName = getKeyList(moduleField, 'showName')
    column.columnInfos.forEach((columnInfo) => {
      formData.value[columnInfo.name] = columnIds.includes(columnInfo.name) ? true : false
      if (moduleFieldName.includes(columnInfo.name.replace('contact_', ''))) {
        columnInfo.showName =
          moduleFieldShowName[moduleFieldName.indexOf(columnInfo.name.replace('contact_', ''))]
      }
    })
  })
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const emit = defineEmits(['success'])
const submitForm = async () => {
  try {
    const columnIds = [] as any[]
    columns.value.forEach(async (column) => {
      column.columnInfos.forEach((columnInfo) => {
        if (formData.value[columnInfo.name]) {
          columnIds.push({ columnName: columnInfo.name })
        }
      })
    })
    if (columnIds.length <= 0) {
      message.error('搜索字段至少选一个')
      return
    }
    await CustomerCheckApi.settingEdit(columnIds)

    message.success(t('common.updateSuccess'))

    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
  }
}
</script>
